﻿using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Controls;
using WPFWorkbookDesigner.Addons;
using WPFWorkbookDesigner.Events;

namespace WPFWorkbookDesigner
{
    public class WindowManager : ViewModelBase
    {
        private static WindowManager _instance;
        private WindowManager()
        {
            //Init();
        }

        public IWorkbookSet BookSet { get; private set; }

        public static WindowManager GetInstance()
        {
            if (_instance != null)
                return _instance;
            _instance = new WindowManager();
            _instance.Init();
            return _instance;
        }

        public void CreateWindow(IWorkbook book)
        {
            MainWindow window = new MainWindow();
            ((App)Application.Current).MainWindows.Add(window);
            window.Closed += WindowOnClosed;
            window.Show();
            Messenger.Default.Send(new OpenWorkbookEvent(book));
            ((App)Application.Current).MainWindow = window;
        }

        private static void WindowOnClosed(object sender, EventArgs eventArgs)
        {
            var window = (MainWindow)sender;
            ((App) Application.Current).MainWindows.Remove(window);
            window.Closed -= WindowOnClosed;
        }

        public override void Cleanup()
        {
            BookSet.Workbooks.Close();
            _instance = null;
            base.Cleanup();
        }

        private void Init(IWorkbookSet bookset)
        {
            BookSet = bookset;
            OnAppStartup(bookset.Workbooks);
        }

        private void Init()
        {
            BookSet = Factory.GetWorkbookSet(CultureInfo.InvariantCulture);
            OnAppStartup(Environment.GetCommandLineArgs());
        }

        private void OnAppStartup(string[] args)
        {
            if (args.Length > 1)
            {
                for (int i = 1; i < args.Length - 1; i++)
                {
                    var path = args[i];
                    try
                    {
                        var book = BookSet.Dowork(s => s.Workbooks.Open(path));
                        CreateWindow(book);
                    }
                    catch (Exception)
                    {
                        //MesBox.Error($"无法打开文件{path}");
                    }
                }
            }
            else
            {
                var books = BookSet.Workbooks;
                var book = BookSet.Dowork(s => s.Workbooks.Add());
                CreateWindow(book);

            }
        }

        private void OnAppStartup(IWorkbooks books)
        {
            foreach (IWorkbook book in books.OfType<IWorkbook>())
            {
                CreateWindow(book);
            }
        }
    }

    public class WindowsHelper
    {
        public static void SetWindowToFront(Window window)
        {
            if(!window.IsVisible)
                window.Show();
            if(window.WindowState == WindowState.Minimized)
                window.WindowState = WindowState.Normal;
            window.Activate();
            window.Topmost = true;
            window.Topmost = false;
            window.Focus();
        }

        public static WorkbookView GetView(Window window)
        {
            var col = ((App) Application.Current).Windows;
            MainWindow mw = window as MainWindow;
            return mw?.WorkbookView;
        }
    }
}
